package com.xmtlxx.web.controller.sacw;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.util.ObjectUtil;
import com.xmtlxx.common.annotation.Log;
import com.xmtlxx.common.annotation.RepeatSubmit;
import com.xmtlxx.common.core.controller.BaseController;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.domain.R;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.core.validate.AddGroup;
import com.xmtlxx.common.enums.BusinessType;
import com.xmtlxx.common.exception.ServiceException;
import com.xmtlxx.common.utils.poi.ExcelUtil;
import com.xmtlxx.sacw.domain.DisposalBankConfig;
import com.xmtlxx.sacw.domain.DisposalInfo;
import com.xmtlxx.sacw.domain.DisposalPro;
import com.xmtlxx.sacw.domain.bo.DisposalConfirmBo;
import com.xmtlxx.sacw.domain.bo.DisposalRegisterBo;
import com.xmtlxx.sacw.domain.bo.DisposalRegisteredBo;
import com.xmtlxx.sacw.domain.vo.DisposalRegisteredInfoVo;
import com.xmtlxx.sacw.service.IDisposalBankConfigService;
import com.xmtlxx.sacw.service.IDisposalService;
import com.xmtlxx.system.domain.SysOss;
import com.xmtlxx.system.enums.OssBuss;
import com.xmtlxx.system.service.ISysOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 处置控制器
 *
 * @author hergreen
 * @date 2024/07/17 16:51
 **/
@Validated
@Api(value = "处置控制器", tags = {"处置管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/sacw/disposal")
public class DisposalController extends BaseController {

    private final IDisposalService iDisposalService;

    private final IDisposalBankConfigService iDisposalBankConfigService;

    private final ISysOssService iSysOssService;

    @ApiOperation("查询处置确认列表")
    @GetMapping("/confirm/list")
    public TableDataInfo<DisposalConfirmBo> getDisposalConfirmPage(PageQuery pageQuery, DisposalConfirmBo bo) {

        return iDisposalService.getDisposalConfirmPage(pageQuery, bo);
    }

    @ApiOperation("新增处置登记")
    @SaCheckPermission("oms:task:add")
    @Log(title = "处置登记", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/register")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> addDisposalRegister(@Validated(AddGroup.class) @RequestBody DisposalRegisterBo bo) {

        iDisposalService.disposalRegister(bo);
        return R.ok();
    }

    @ApiOperation("查询处置已登记列表")
    @GetMapping("/registered/list")
    public TableDataInfo<DisposalRegisteredBo> getDisposalRegisteredPage(PageQuery pageQuery, DisposalRegisteredBo bo) {

        return iDisposalService.getDisposalRegisteredPage(pageQuery, bo);
    }

    @PostMapping("/register/upload")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> uploadExcel(@RequestParam("file") MultipartFile file) {
        try {
            List<List<String>> data = ExcelUtil.readExcel(file);
            iDisposalService.uploadExcel(data);
            return R.ok();
        } catch (Exception e) {
            return R.fail("处理失败: " + e.getMessage());
        }
    }

    @ApiOperation("查询处置已登记信息")
    @GetMapping("/registered/{id}")
    public R<DisposalRegisteredInfoVo> getDisposalRegisteredInfo(@PathVariable("id") String id) {

        return R.ok(iDisposalService.getDisposalRegisteredInfo(id));
    }

    @ApiOperation("删除处置已登记信息")
    @Log(title = "处置登记", businessType = BusinessType.DELETE)
    @DeleteMapping("/registered/{id}")
    public R<DisposalRegisteredInfoVo> deleteDisposalRegistered(@PathVariable("id") String id) {

        iDisposalService.deleteDisposalRegistered(id);
        return R.ok();
    }

    @ApiOperation("更新处置信息")
    @Log(title = "处置信息", businessType = BusinessType.UPDATE)
    @PostMapping("/info")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> updateInfo(@RequestBody DisposalInfo disposalInfo) {

        return iDisposalService.updateInfo(disposalInfo) ? R.ok() : R.fail();
    }

    @Log(title = "处置附件", businessType = BusinessType.INSERT)
    @PostMapping("/annex")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> uploadAnnex(@RequestPart("file") MultipartFile file, String objid) {

        if (ObjectUtil.isNull(file)) {
            throw new ServiceException("上传文件不能为空");
        }
        SysOss oss = iSysOssService.upload(file, null, OssBuss.DISPOSAL_REGISTER.getValue(), objid);

        return oss != null ? R.ok() : R.fail();
    }

    @Log(title = "处置附件", businessType = BusinessType.UPDATE)
    @DeleteMapping("/annex/{id}")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> deleteAnnex(@PathVariable Long id) {

        return toAjax(iSysOssService.deleteWithValidByIds(Collections.singletonList(id), false));
    }

    @Log(title = "处置财物", businessType = BusinessType.UPDATE)
    @PutMapping("/pro")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> addPro(@RequestBody List<DisposalPro> disposalProList) {

        return toAjax(iDisposalService.addPro(disposalProList));
    }

    @Log(title = "处置财物", businessType = BusinessType.UPDATE)
    @DeleteMapping("/pro/{id}")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> deletePro(@PathVariable String id) {

        return toAjax(iDisposalService.deletePro(id));
    }

    @ApiOperation("处置银行配置更新")
    @Log(title = "处置银行配置", businessType = BusinessType.INSERT)
    @PostMapping("/bankConfig")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> bankConfigInsertOrUpdate(@RequestBody DisposalBankConfig config) {

        return iDisposalBankConfigService.insertOrUpdate(config) ? R.ok() : R.fail();
    }

    @ApiOperation("查询处置银行配置列表")
    @GetMapping("/bankConfig/list")
    public R<List<DisposalBankConfig>> getDisposalBankConfigList() {

        return R.ok(iDisposalBankConfigService.getDisposalBankConfigList());
    }

    @ApiOperation("处置银行配置更新")
    @Log(title = "处置银行配置", businessType = BusinessType.DELETE)
    @DeleteMapping("/bankConfig/{id}")
    @Transactional(rollbackFor = Exception.class)
    public R<Void> bankConfigDelete(@PathVariable("id") String id) {

        return iDisposalBankConfigService.delete(id) ? R.ok() : R.fail();
    }

}
